package com.xy.exam.service.impl;

import com.xy.exam.Exception.ServiceException;
import com.xy.exam.dao.mapper.InfoMapper;
import com.xy.exam.pojo.dto.InfoAddNewDTO;
import com.xy.exam.pojo.dto.InfoUpdateDTO;
import com.xy.exam.pojo.entity.Info;
import com.xy.exam.pojo.entity.User;
import com.xy.exam.pojo.vo.InfoListItemVO;
import com.xy.exam.pojo.vo.InfoStandardVO;
import com.xy.exam.pojo.vo.UserListItemVO;
import com.xy.exam.pojo.vo.UserStandardVO;
import com.xy.exam.service.IInfoService;
import com.xy.exam.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class InfoServiceImpl implements IInfoService {
    @Autowired
    InfoMapper infoMapper;
    @Override
    public void addNew(InfoAddNewDTO infoAddNewDTO) {
        String title = infoAddNewDTO.getTitle();
        int countByTitle = infoMapper.countByTitle(title);
        if (countByTitle > 0) {
            String message = "添加考试介绍失败,该考试标题已经存在!";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        Info info = new Info();
        info.setCreateTime(new Date());
        info.setUpdateTime(new Date());
        BeanUtils.copyProperties(infoAddNewDTO, info);
        int insert = infoMapper.insert(info);
        if (insert != 1) {
            String message = "添加考试介绍失败,该考试标题已经存在!";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        InfoStandardVO standardById = infoMapper.getStandardById(id);
        if (standardById == null) {
            String message = "删除考试介绍失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        int deleteById = infoMapper.deleteById(id);
        if (deleteById !=1) {
            String message = "删除考试介绍失败，服务器忙，请稍后再尝试!";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(Long id, InfoUpdateDTO infoUpdateDTO) {
        InfoStandardVO queryResult = infoMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "修改考试介绍详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        String name = infoUpdateDTO.getTitle();
        int count = infoMapper.countByTitleAndNotId(id, name);
        if (count > 0) {
            String message = "修改考试介绍详情失败，考试介绍名称已经被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Info info = new Info();
        info.setUpdateTime(new Date());
        info.setId(id);
        BeanUtils.copyProperties(infoUpdateDTO,info);
        int rows = infoMapper.update(info);
        if (rows != 1) {
            String message = "修改考试介绍详情失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public InfoStandardVO getStandardById(Long id) {
        InfoStandardVO queryResult = infoMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "获取用户详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public List<InfoListItemVO> list() {
        List<InfoListItemVO> list = infoMapper.list();
        return list;
    }
}
